﻿Shader "Unlit/Blur"
{
    Properties
    {
        _SoftEdge("边缘软边", Range(0, 0.9)) = 0.5
        _Step("Step", Float) = 0.01
        _Size("Size", Float) = 2
    }
    SubShader
    {
        Tags { "RenderType"="Transparent" "Queue"="Transparent"}
        LOD 100

        GrabPass{}
		ZWrite Off

        Pass
        {
            Blend SrcAlpha OneMinusSrcAlpha

            CGPROGRAM
            #pragma vertex vert
            #pragma fragment frag

            #include "UnityCG.cginc"

            struct appdata
            {
                float4 vertex : POSITION;
                float2 uv : TEXCOORD0;
            };

            struct v2f
            {
                float2 uv : TEXCOORD0;
                float4 proj : TEXCOORD1;
                float4 vertex : SV_POSITION;
            };

            half _SoftEdge;

            float _Step;
            float _Size;

            sampler2D _CameraDepthTexture;
            sampler2D _GrabTexture;

            v2f vert (appdata v)
            {
                v2f o;
                o.vertex = UnityObjectToClipPos(v.vertex);
                o.proj = ComputeScreenPos(o.vertex);
                o.uv = v.uv;
                return o;
            }

            fixed4 frag (v2f i) : SV_Target
            {
                float2 uv = i.proj.xy/i.proj.w;

                float4 color = float4(0, 0, 0, 1);
                _Size = (int)_Size;

                for (int x = -_Size; x <= _Size; x++)
                {
                    for (int y = -_Size; y <= _Size; y++)
                    {
                        color.rgb += tex2D(_GrabTexture, uv + float2(x, y) * _Step).rgb;
                    }
                }

                color.rgb /= (_Size * 2 + 1) * (_Size * 2 + 1);

                color.a *= smoothstep(0, _SoftEdge, i.uv.y);
                color.a *= smoothstep(0, _SoftEdge, 1 - i.uv.y);
                color.a *= smoothstep(0, _SoftEdge, i.uv.x);
                color.a *= smoothstep(0, _SoftEdge, 1 - i.uv.x);

                return color;
            }
            ENDCG
        }
    }
}
